﻿using SindraxU3DFrameWork.Scripts.Bases;

using UnityEngine;

namespace Assets.Scripts.Pooling.Imp
{
    class MonoPooling<E> : MonoBehaviour, IPooling<E> where E : MonoBehaviour
    {
        public IPooling<E> Pooling { get; private set; }
        public GameObject Prefab { get; private set; }

        public void InitPooling(GameObject prefab)
        {
            Pooling = new ListPooling<E>
            (
                e => !e.gameObject.activeSelf,
                e => e.gameObject.SetActive(true),
                e => e.gameObject.SetActive(false),
                () =>
                {
                    GameObject instan = Instantiate(Prefab);
                    instan.transform.parent = transform;
                    return instan.GetComponent<E>();
                }
            );
        }


        public void EmptyPooling()
        {
            Pooling.EmptyPooling();
            for (int i = 0; i < transform.childCount; i++)
            {
                Destroy(transform.GetChild(i));
            }
        }

        public E GetSpawn() => Pooling.GetSpawn();

        public void RecycelSpawn(E e) => Pooling.RecycelSpawn(e);

        public void RecycleAllSpawn() => Pooling.RecycleAllSpawn();
    }
}
